package com.zhupanlin.zhyl.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zhupanlin.zhyl.hosp.repository.DepartmentRepository;
import com.zhupanlin.zhyl.hosp.service.DepartmentService;
import com.zhupanlin.zhyl.hosp.service.HospitalService;
import com.zhupanlin.zhyl.model.hosp.Department;
import com.zhupanlin.zhyl.model.vo.hosp.DepartmentQueryVo;
import com.zhupanlin.zhyl.model.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/11/24 13:20
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private HospitalService hospitalService;

    /**
     * 添加科室信息
     *
     * @param paramMap
     */
    @Override
    public void save(Map<String, Object> paramMap) {
        hospitalService.checkSign(paramMap);
        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);

        Department departmentExists = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        // 根据医院编号和科室编号查询
        if (departmentExists != null) {
            department.setUpdateTime(new Date());
            department.setIsDeleted(departmentExists.getIsDeleted());
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
        }
        departmentRepository.save(department);
    }

    /**
     * 查询科室列表信息
     *
     * @param paramMap
     * @return
     */
    @Override
    public Page<Department> findDepartmentList(Map<String, Object> paramMap) {
        hospitalService.checkSign(paramMap);

        String hoscode = (String) paramMap.get("hoscode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        // 分页对象
        Pageable pageable = PageRequest.of(page, limit);

        // Example 对象，条件
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase();
        Example<Department> example = Example.of(department, matcher);

        Page<Department> pageInfo = departmentRepository.findAll(example, pageable);

        return pageInfo;
    }

    /**
     * 删除科室
     *
     * @param paramMap
     */
    @Override
    public void removeDepartment(Map<String, Object> paramMap) {
        hospitalService.checkSign(paramMap);

        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        // 根据医院编号和科室编号查询科室信息
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            // 删除
            departmentRepository.deleteById(department.getId());
        }

    }

    /**
     * 根据医院编号，查询医院所有科室列表
     *
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> result = new ArrayList<>();
        // 根据医院编号，查询医院所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        // 所有科室列表
        List<Department> departmentList = departmentRepository.findAll(example);

        // 根据大科室编号 bigcode 进行分组，获取每个大可是里面的下级子科室
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            String bigCode = entry.getKey();
            List<Department> departList = entry.getValue();

            // 封装大科室
            DepartmentVo bigDepartmentVo = new DepartmentVo();
            bigDepartmentVo.setDepcode(bigCode);
            bigDepartmentVo.setDepname(departList.get(0).getBigname());

            // 封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : departList) {
                DepartmentVo departmentVo = new DepartmentVo();
                departmentVo.setDepcode(department.getDepcode());
                departmentVo.setDepname(department.getDepname());
                children.add(departmentVo);
            }
            bigDepartmentVo.setChildren(children);
            result.add(bigDepartmentVo);
        }
        return result;
    }

    /**
     * 根据科室编号 和 医院编号 查询科室名称
     *
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            return department.getDepname();
        }
        return null;
    }

    /**
     * 根据科室编号 和 医院编号 查询科室
     *
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
